package com.atguigu.gulimall.juc.demo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class PCDemoData1{
    private volatile int number = 1; // A:1 B:2 C:3
    private Lock lock = new ReentrantLock();
    Condition cA = lock.newCondition();
    Condition cB = lock.newCondition();
    Condition cC = lock.newCondition();
    public void print5(){
        lock.lock();
        try {
            while (number != 1){
                cA.await();
            }
            number++;
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            number=2;
            cB.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void print10(){
        lock.lock();
        try {
            while (number != 2){
                cB.await();
            }
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            number=3;
            cC.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void print15(){
        lock.lock();
        try {
            while (number != 3){
                cC.await();
            }
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            number=1;
            cA.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
public class PCDemo1 {
    public static void main(String[] args) {
        PCDemoData1 data = new PCDemoData1();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.print5();
            }
        },"AAA").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.print10();
            }
        },"BBB").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.print15();
            }
        },"CCC").start();
    }
}
